﻿Imports DevExpress.XtraTreeList.Nodes.Operations
Public Class ExplorerTreeListState
    Private expanded As ArrayList
    Private selected As ArrayList
    Private focused As Object
    Private topIndex As Integer

    Public Sub New()
        Me.New(Nothing)
    End Sub
    Public Sub New(ByVal treeList As TreeList)
        Me.treeList_Renamed = treeList
        expanded = New ArrayList()
        selected = New ArrayList()
    End Sub

    Public Sub Clear()
        expanded.Clear()
        selected.Clear()
        focused = Nothing
        topIndex = 0
    End Sub
    Private Function GetExpanded() As ArrayList
        Dim op As New OperationSaveExpanded()
        TreeList.NodesIterator.DoOperation(op)
        Return op.Nodes
    End Function
    Private Function GetSelected() As ArrayList
        Dim al As New ArrayList()
        For Each node As TreeListNode In TreeList.Selection
            al.Add(node.GetValue(TreeList.KeyFieldName))
        Next node
        Return al
    End Function

    Public Sub LoadState()
        Dim node As TreeListNode

        If TreeList.AutoFilterNodeId = TreeList.FocusedNode.Id Then
            TreeList.Nodes(0).RootNode.Expanded = True
            Return
        End If

        TreeList.BeginUpdate()
        Try
            TreeList.CollapseAll()
            For Each key As Object In expanded
                node = TreeList.FindNodeByKeyID(key)
                If node IsNot Nothing Then
                    node.Expanded = True
                End If
            Next key
            For Each key As Object In selected
                node = TreeList.FindNodeByKeyID(key)
                If node IsNot Nothing Then
                    TreeList.Selection.Add(node)
                End If
            Next key
            TreeList.FocusedNode = TreeList.FindNodeByKeyID(focused)
        Finally
            TreeList.EndUpdate()
            TreeList.TopVisibleNodeIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - topIndex
            '
            'Test point: Ici ca va nous ramener les childs que nous avons p-e ajouter a droite sous le denier noeud que nous étions positionné. 
            If TreeList.FocusedNode.HasChildren AndAlso TreeList.FocusedNode.Level > 0 Then
                node = TreeList.FocusedNode
                If node.Expanded Then
                    While node.HasChildren
                        node.ExpandAll()
                        node = node.FirstNode
                    End While
                End If
            End If
            '
        End Try
    End Sub
    Public Sub SaveState()
        If TreeList.AutoFilterNodeId = TreeList.FocusedNode.Id Then Return

        If TreeList.FocusedNode IsNot Nothing Then
            expanded = GetExpanded()
            selected = GetSelected()
            focused = TreeList.FocusedNode(TreeList.KeyFieldName)
            topIndex = TreeList.GetVisibleIndexByNode(TreeList.FocusedNode) - TreeList.TopVisibleNodeIndex
        Else
            Clear()
        End If
    End Sub

    Private treeList_Renamed As TreeList
    Public Property TreeList() As TreeList
        Get
            Return treeList_Renamed
        End Get
        Set(ByVal value As TreeList)
            treeList_Renamed = value
            Clear()
        End Set
    End Property

    Private Class OperationSaveExpanded
        Inherits TreeListOperation
        Private al As New ArrayList()
        Public Overrides Sub Execute(ByVal node As TreeListNode)
            If node.HasChildren AndAlso node.Expanded Then
                al.Add(node.GetValue(node.TreeList.KeyFieldName))
            End If
        End Sub
        Public ReadOnly Property Nodes() As ArrayList
            Get
                Return al
            End Get
        End Property
    End Class
End Class
